const mongoose = require('./db');

// 轮播图表
let lunboSchema = new mongoose.Schema({
    img: Array,//图片
})
let lunboModel = mongoose.model('lunbo', lunboSchema, 'lunbo')


const pcuserSchema = new mongoose.Schema({
    username: String,
    password: String,
    // 姓名
    name: {
        type: String,
        default: "",
    },
    // 是否启用
    status: {
        type: Number,
        default: 0,
    },
    //创建时间
    createdAt: {
        type: Date,
        default: Date.now,
    },
    //更新时间
    updatedAt: {
        type: Date,
        default: Date.now,
    },
    //   角色
    role_id: {
        type: mongoose.Types.ObjectId,
        ref: "role",
    },
});


const roleSchema = new mongoose.Schema({
    // 角色名称
    name: {
        type: String,
        default: true,
    },
    // 角色描述
    description: {
        type: String,
        default: "",
    },
    // 菜单权限
    menus: {
        type: [mongoose.Types.ObjectId],
        ref: "menu",
    },
    // 是否启用
    status: {
        type: Number,
        default: 0,
    },
    //创建时间
    createdAt: {
        type: Date,
        default: Date.now,
    },
    //更新时间
    updatedAt: {
        type: Date,
        default: Date.now,
    },
});
const menuSchema = new mongoose.Schema({
    // 菜单名称
    name: {
        type: String,
        default: true,
    },
    // 菜单描述
    description: {
        type: String,
        default: "",
    },
    // 上级菜单
    parentId: {
        type: mongoose.Types.ObjectId,
        ref: "menu",
    },
    //   前端名称
    unique: {
        type: String,
        unique: true,
        required: true,
    },
    //   菜单图标
    icon: {
        type: String,
        default: "",
    },
    //   是否显示
    hidden: {
        type: Boolean,
        default: false,
    },
    // 是否启用
    status: {
        type: Number,
        default: 0,
    },
    sort: {
        type: Number,
        default: 0,
    },
    //创建时间
    createdAt: {
        type: Date,
        default: Date.now,
    },
    //更新时间
    updatedAt: {
        type: Date,
        default: Date.now,
    },
});

const ordermanagementSchema = new mongoose.Schema({
    hao: String,
    time: {
        type: Date,
        default: Date.now,
    },
    tuan: String,
    xiaren: String,
    shouren: String,
    phone: String,
    count: Number,
    price: Number,
    status: Number,
})
const usersSchema = new mongoose.Schema({
    time: String,
    adduser: Number,
    dayadd: Number,
    zhouadd: Number,
    yueadd: Number,
    dongadd: Number,
    acgtime: String
});
const users1Schema = new mongoose.Schema({
    time: String,
    q1: Number,
    q2: Number,
    q3: Number,
    q4: Number,
    q5: Number,
    q6: String
});

const userSchma = new mongoose.Schema({
    username: String, //用户名
    password: String, //密码
    email: String, //邮箱
    img: String, //头像
    name: String, //姓名
    phone: String,  //手机号
})


// 商品表
const listSchema = new mongoose.Schema({
    name: String,//商品名称
    original: Number,//商品原价
    price: Number,//商品售价
    num: Number,//商品库存
    stag: {//商品标签
        type: mongoose.Types.ObjectId,
        ref: 'stag'
    },
    cid: {//商品类别
        type: mongoose.Types.ObjectId,
        ref: 'cate'
    },
    img: Array,//图片
    video: String,//商品视频
    state: Boolean,//商品状态
    sold: Number, //已售
    brand: String, //品牌
    specif: String, //规格
    date: Date, //生产日期
    period: Number, //保质期
    status: {
        type: Number,
        default: 1
    },
})
//商品标签表
const stagScheme = new mongoose.Schema({
    name: String,//标签名称
    description: String,//标签描述
    state: Boolean,
})
//商品分类
const cateSchema = new mongoose.Schema({
    title: String,//分类名称,
    state: String,//分类状态，
    img: String,//分类图片
    fid: {
        type: mongoose.Types.ObjectId,
        ref: 'cate',
        required: false,
    }
})
const userModel = mongoose.model('user', userSchma, 'user');
// 分类表
let cateModel = mongoose.model('cate', cateSchema, 'cate');
let listModel = mongoose.model('list', listSchema, 'list')
// 用户表
const loginSchema = new mongoose.Schema({
    loginname: String,//用户名
    loginpass: String,//登录电话
})
const loginModel = mongoose.model('login', loginSchema, 'login')
// 购物车表
let shopSchema = new mongoose.Schema({
    name: String, //商品名称
    original: Number,  //原价
    price: Number,  //现价
    img: Array,  //图片
    sold: Number,  //已售
    brand: String,  //品牌
    specif: String,  //规格
    date: String,  //生产日期
    period: String,  //保质期
    num: Number,  //数量
    status: {
        type: Number,
        default: 1
    },
    shopid: {
        type: mongoose.Types.ObjectId,
        ref: 'list'
    }
})
let shopModel = mongoose.model('shop', shopSchema, 'shop')
// 订单表
let dingSchema = new mongoose.Schema({
    name: String, //商品名称
    original: Number,  //原价
    price: Number,  //现价
    img: Array,  //图片
    sold: Number,  //已售
    brand: String,  //品牌
    specif: String,  //规格
    date: String,  //生产日期
    period: String,  //保质期
    num: Number,  //数量
    status: {
        type: Number,
        default: 1
    },
    cid: {
        type: mongoose.Types.ObjectId,
        ref: 'cate'
    }
})
let dingModel = mongoose.model('ding', dingSchema, 'ding')



// 优惠券表
const couponSchema = new mongoose.Schema({
    status: {
        type: Number,
        enum: [0, 1, 2], // 0: 未使用, 1: 已使用, 2: 已过期
        default: 0
    },
    name: {
        type: String,
        required: true
    },
    amount: {//优惠金额
        type: Number,
        required: true
    },
    startDate: {
        type: Date,
        required: true
    },
    endDate: {
        type: Date,
        required: true
    },
    // 最少需要消费金额
    minimumAmount: {
        type: Number,
        required: true
    }
});


// 订单表
const orderSchema = new mongoose.Schema({
    status: {//订单状态
        type: Number,
        enum: [0, 1, 2, 3], // 0: 待付款, 1: 到取货, 2: 已完成,3:已取消
        default: 0
    },
    uid: {//下单用户
        type: mongoose.Types.ObjectId,
        ref: 'user',
    },
    shopid: [{//商品数组
        type: mongoose.Types.ObjectId,
        ref: 'list',
    }],
    price: Number,//商品金额
    yprice: Number,//商品优惠
    num: Number,//商品数量
    sprice: Number,//实付金额
    address: String,//订单地址
    time: Date,//下单时间
})

const Coupon = mongoose.model('Coupon', couponSchema, 'Coupon');

const stag = mongoose.model('stag', stagScheme, 'stag')//商品标签表

const pcuser = mongoose.model("pcuser", pcuserSchema, "pcuser");
const role = mongoose.model("role", roleSchema, "role");
const menu = mongoose.model("menu", menuSchema, "menu");
const ordermanagement = mongoose.model("ordermanagement", ordermanagementSchema, "ordermanagement");
const order = mongoose.model("order", orderSchema, "order");

const users = mongoose.model("users", usersSchema, "users");
const users1 = mongoose.model("users1", users1Schema, "users1");

module.exports = {
    pcuser,
    role,
    menu,
    ordermanagement,
    listModel,
    loginModel,
    stag,
    cateModel,
    Coupon,
    pcuser,
    order,
    users,
    users1,
    userModel, shopModel, dingModel, lunboModel
}
